from models.user import User
from models.weibo import Weibo
from models.weibo import Comment
from .session import session
from utils import error
from utils import redirect
from utils import log
from utils import http_response
from utils import template
from utils import response_with_headers


def current_user(request):
    session_id = request.cookies.get('user', '')
    user_id = session.get(session_id, -1)
    return user_id


def edit(request):
    weibo_id = request.query.get('id', -1)
    weibo_id = int(weibo_id)
    w = Weibo.find(weibo_id)
    if w is None:
        return error(request)
    # 生成一个 edit 页面
    body = template('weibo_edit.html',
                    weibo_id=w.id,
                    weibo_content=w.content)
    return http_response(body)


def index(request):
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    user = User.find(user_id)
    if user is None:
        return redirect('/login')
    weibos = Weibo.find_all(user_id=user_id)
    body = template('weibo_index.html', weibos=weibos, user=user)
    return http_response(body)


def new(request):
    uid = current_user(request)
    user = User.find(uid)
    body = template('weibo_new.html')
    return http_response(body)


def add(request):
    uid = current_user(request)
    user = User.find(uid)
    form = request.form()
    w = Weibo(form)
    w.user_id = user.id
    w.save()
    return redirect('/weibo/index?user_id={}'.format(user.id))


def update(request):
    username = current_user(request)
    user = User.find_by(username=username)
    form = request.form()
    content = form.get('content', '')
    weibo_id = int(form.get('id', -1))
    w = Weibo.find(weibo_id)
    if user.id != w.user_id:
        return error(request)
    w.content = content
    w.save
    return redirect('/weibo/index?user_id={}'.format(user.id))


def delete(request):
    uid = current_user(request)
    user = User.find(uid)
    weibo_id = request.query.get('id', None)
    weibo_id = int(weibo_id)
    w = Weibo.find(weibo_id)
    w.delete()
    return redirect('/weibo/index?user_id={}'.format(user.id))


def login_required(route_function):
    def func(request):
        uid = current_user(request)
        log('登录鉴定,user_id', uid)
        if uid == -1:
            return redirect('/login')
        else:
            return route_function(request)

    return func


def comment_add(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uid = current_user(request)
    header = response_with_headers(headers)
    user = User.find(uid)
    form = request.form()
    w = Comment(form)
    w.user_id = user.id
    w.save()
    return redirect('/weibo/index?user_id={}'.format(user.id))


route_dict = {
    '/weibo/index': index,
    '/weibo/new': login_required(new),
    '/weibo/add': login_required(add),
    '/weibo/edit': login_required(edit),
    '/weibo/update': login_required(update),
    '/weibo/delete': login_required(delete),
    '/comment/add': login_required(comment_add),
}
